package lesson3;

import org.testng.Assert;
import org.testng.annotations.*;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HomeTask2 {
    final String filename = "C:\\test\\file_for_HomeTask2.txt";

    @BeforeTest
    @Parameters({"string1", "string2", "string3"})

    public void beforeTest(@Optional("10dsfdfdsff554dsfdfs") String string1,
                           @Optional("wweffff33dsf66ddg") String string2,
                           @Optional("dsdsfs10000ddssc88fcf") String string3) throws Exception {
        File myFile = new File(filename);
        if (!myFile.exists()) {
            myFile.createNewFile();
            System.out.println("File " + filename + " create!!!");
        }

        PrintWriter out = new PrintWriter(myFile.getAbsoluteFile());
        try {
            out.println(string1);
            out.println(string2);
            out.println(string3);
            System.out.println("File " + filename + " add 3 string!!!");
        } finally {
            out.close();
        }
    }


    @Test
    public void testHomeTask2() throws IOException {
        FileReader fileReader = new FileReader(filename);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        List lines = new ArrayList();
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            Pattern pt = Pattern.compile("\\d+");
            Matcher matcher = pt.matcher(line);
            matcher.find();
            lines.add(Integer.parseInt(matcher.group()));
        }
        bufferedReader.close();

        List linesSort = new ArrayList(lines);
        Collections.sort(linesSort);
        Assert.assertEquals(linesSort, lines,"Sort!!!");

    }

    @AfterTest
    public void afterTest() {
        File myFile = new File(filename);

        if (myFile.exists()) {
            myFile.delete();
            System.out.println("File deleted!!!");
        }
        else System.out.println("File not found!!!");
    }

}
